package com.drinkdelivery.common.token;

import cn.hutool.core.util.StrUtil;
import com.drinkdelivery.common.constants.Constants;
import com.drinkdelivery.common.constants.RedisConstants;
import com.drinkdelivery.common.domain.Users;
import com.drinkdelivery.common.utils.FrontRedisUtil;
import com.drinkdelivery.common.utils.RequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * token验证处理
 */
@Service
public class TokenComponent {

    @Resource
    private FrontRedisUtil frontRedisUtil;

    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;

    private static final Long MILLIS_MINUTE = 60 * 1000L;

    // 令牌有效期（默认30分钟） todo 调试期改为5小时
//    private static final int expireTime = 30;
    private static final int expireTime = 5 * 60;

    /**
     * 删除用户身份信息
     */
    public void delLoginUser(String token) {
        if (StrUtil.isNotBlank(token)) {
            String userKey = getTokenKey(token);
            frontRedisUtil.delete(userKey);
        }
    }

    /**
     * 创建令牌
     *
     * @param user 用户信息
     * @return 令牌
     */
    public String createToken(Users user) {
        String token = UUID.randomUUID().toString().replace("-", "");
        frontRedisUtil.set(getTokenKey(token), user.getId(), Constants.TOKEN_EXPRESS_MINUTES, TimeUnit.MINUTES);
        return token;
    }

    /**
     * 获取请求token
     *
     * @param request HttpServletRequest
     * @return token
     */
    public String getToken(HttpServletRequest request) {
        String token = request.getHeader(Constants.HEADER_AUTHORIZATION_KEY);
        if (StrUtil.isNotBlank(token) && token.startsWith(RedisConstants.USER_TOKEN_REDIS_KEY_PREFIX)) {
            token = token.replace(RedisConstants.USER_TOKEN_REDIS_KEY_PREFIX, "");
        }
        return token;
    }

    public String getTokenKey(String uuid) {
        return RedisConstants.USER_TOKEN_REDIS_KEY_PREFIX + uuid;
    }

    /**
     * 推出登录
     *
     * @param request HttpServletRequest
     */
    public void logout(HttpServletRequest request) {
        String token = getToken(request);
        delLoginUser(token);
    }

    /**
     * 获取当前登录用户id
     */
    public Integer getUserId() {
        HttpServletRequest request = RequestUtil.getRequest();
        String token = getToken(request);
        if (StrUtil.isEmpty(token)) {
            return null;
        }
        return frontRedisUtil.get(getTokenKey(token));
    }

    public Boolean check(String token) {

        try {
            boolean exists = frontRedisUtil.exists(getTokenKey(token));
            if (exists) {
                Integer uid = frontRedisUtil.get(getTokenKey(token));
                frontRedisUtil.set(getTokenKey(token), uid, Constants.TOKEN_EXPRESS_MINUTES, TimeUnit.MINUTES);
            }
            return exists;
        } catch (Exception e) {
            return false;
        }
    }
}
